﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class PutawayRuleDetailBLL : IBaseInfoDetail
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IFilterConfigDetailDA filterConfigDetail = DataAccess.CreateFilterConfigDetailDA();
        private PubParasInfo pubParasInfo;
        private static readonly IPutawayRuleDetailDA putawayDetailRuleDA = DataAccess.CreatePutawayRuleDetailDA();

        public PutawayRuleDetailBLL()
        {
            this.pubParasInfo = null;
        }

        public PutawayRuleDetailBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public PutawayRuleDetailBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteDetail(List<string> mainID, List<string> subId, List<string> pk3)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                if (mainID.Count != subId.Count)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2410_005", this.pubParasInfo.UserLanguage);
                }
                List<PutawayRuleDetailInfo> putawayRuleDetailInfos = new List<PutawayRuleDetailInfo>();
                PutawayRuleDetailInfo putawayRuleDetailInfo = null;
                for (i = 0; i < mainID.Count; i++)
                {
                    putawayRuleDetailInfo = putawayDetailRuleDA.GetPutawayRuleDetailByID(dataBase, tran, mainID[i], subId[i], this.dataBaseID[0], this.pubParasInfo);
                    putawayRuleDetailInfos.Add(putawayRuleDetailInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (putawayDetailRuleDA.DeletePutawayRuleDetail(dataBase, tran, mainID, subId, this.dataBaseID) > 0)
                {
                    for (i = 0; i < mainID.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<PutawayRuleDetailInfo>(dataBase, tran, putawayRuleDetailInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "PUTAWAY_RULE_DETAIL", this.dataBaseID[0]);
                        string CS00002 = temp;
                        temp = CS00002 + mainID[i] + "," + subId[i] + ";";
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllDetailInfo()
        {
            return putawayDetailRuleDA.GetAllPutawayRuleDetail(this.dataBaseID[0], this.pubParasInfo);
        }

        public DataSet GetDetaiInfolByQueryEntity(object obj)
        {
            PutawayRuleDetailQueryEntity putawayRuleDetail = obj as PutawayRuleDetailQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(putawayRuleDetail.PutawayRuleID))
            {
                sqlWhere.Add(" PUTAWAY_RULE_ID " + putawayRuleDetail.PutawayRuleID);
            }
            if (!string.IsNullOrEmpty(putawayRuleDetail.LineID))
            {
                sqlWhere.Add(" LINE_ID " + putawayRuleDetail.LineID);
            }
            if (!string.IsNullOrEmpty(putawayRuleDetail.PaStrategy))
            {
                sqlWhere.Add(" PA_STRATEGY " + putawayRuleDetail.PaStrategy);
            }
            if (!string.IsNullOrEmpty(putawayRuleDetail.FromLoc))
            {
                sqlWhere.Add(" FROM_LOC " + putawayRuleDetail.FromLoc);
            }
            if (!string.IsNullOrEmpty(putawayRuleDetail.ToLoc))
            {
                sqlWhere.Add(" TO_LOC " + putawayRuleDetail.ToLoc);
            }
            if (!string.IsNullOrEmpty(putawayRuleDetail.Zone))
            {
                sqlWhere.Add(" ZONE " + putawayRuleDetail.Zone);
            }
            if (!string.IsNullOrEmpty(putawayRuleDetail.DimRestriction01))
            {
                sqlWhere.Add(" DIM_RESTRICTION01 " + putawayRuleDetail.DimRestriction01);
            }
            if (!string.IsNullOrEmpty(putawayRuleDetail.DimRestriction02))
            {
                sqlWhere.Add(" DIM_RESTRICTION02 " + putawayRuleDetail.DimRestriction02);
            }
            if (!string.IsNullOrEmpty(putawayRuleDetail.DimRestriction03))
            {
                sqlWhere.Add(" DIM_RESTRICTION03 " + putawayRuleDetail.DimRestriction03);
            }
            if (!string.IsNullOrEmpty(putawayRuleDetail.DimRestriction04))
            {
                sqlWhere.Add(" DIM_RESTRICTION04 " + putawayRuleDetail.DimRestriction04);
            }
            if (!string.IsNullOrEmpty(putawayRuleDetail.DimRestriction05))
            {
                sqlWhere.Add(" DIM_RESTRICTION05 " + putawayRuleDetail.DimRestriction05);
            }
            if (!string.IsNullOrEmpty(putawayRuleDetail.DimRestriction06))
            {
                sqlWhere.Add(" DIM_RESTRICTION06 " + putawayRuleDetail.DimRestriction06);
            }
            if (!string.IsNullOrEmpty(putawayRuleDetail.LocStateRestriction01))
            {
                sqlWhere.Add(" LOC_STATE_RESTRICTION01 " + putawayRuleDetail.LocStateRestriction01);
            }
            if (!string.IsNullOrEmpty(putawayRuleDetail.LocStateRestriction02))
            {
                sqlWhere.Add(" LOC_STATE_RESTRICTION02 " + putawayRuleDetail.LocStateRestriction02);
            }
            if (!string.IsNullOrEmpty(putawayRuleDetail.LocStateRestriction03))
            {
                sqlWhere.Add(" LOC_STATE_RESTRICTION03 " + putawayRuleDetail.LocStateRestriction03);
            }
            if (!string.IsNullOrEmpty(putawayRuleDetail.LocStateRestriction04))
            {
                sqlWhere.Add(" LOC_STATE_RESTRICTION04 " + putawayRuleDetail.LocStateRestriction04);
            }
            if (!string.IsNullOrEmpty(putawayRuleDetail.LocStateRestriction05))
            {
                sqlWhere.Add(" LOC_STATE_RESTRICTION05 " + putawayRuleDetail.LocStateRestriction05);
            }
            if (!string.IsNullOrEmpty(putawayRuleDetail.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + putawayRuleDetail.IsActive);
            }
            return putawayDetailRuleDA.GetPutawayRuleDetailByQueryList(sqlWhere, putawayRuleDetail, this.dataBaseID[0], this.pubParasInfo);
        }

        public object GetDetailInfoByID(string mainID, string subID, string pk3)
        {
            return putawayDetailRuleDA.GetPutawayRuleDetailByID(mainID, subID, this.dataBaseID[0], this.pubParasInfo);
        }

        public List<object> GetDetailInfoListByID(string mainID, string subID)
        {
            List<PutawayRuleDetailInfo> putawayRuleDetail = putawayDetailRuleDA.GetPutawayRuleDetailListByID(mainID, this.dataBaseID[0], this.pubParasInfo);
            List<object> objs = null;
            if ((putawayRuleDetail != null) && (putawayRuleDetail.Count > 0))
            {
                objs = new List<object>();
                for (int i = 0; i < putawayRuleDetail.Count; i++)
                {
                    objs.Add(putawayRuleDetail[i]);
                }
            }
            return objs;
        }

        public string InsertDetail(object obj)
        {
            PutawayRuleDetailInfo putawayRuleDetailInfo = obj as PutawayRuleDetailInfo;
            string LineID = string.Empty;
            try
            {
                if (putawayDetailRuleDA.CheckPutawayRuleDetailIDUnique(putawayRuleDetailInfo.PutawayRuleID, putawayRuleDetailInfo.LineID.ToString(), this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2410_002", this.pubParasInfo.UserLanguage, new List<string> { putawayRuleDetailInfo.PutawayRuleID, putawayRuleDetailInfo.LineID.ToString() });
                }
                if (!string.IsNullOrEmpty(putawayRuleDetailInfo.LocSelection) && !filterConfigDetail.CheckFilterConfigDetailIDUnique(putawayRuleDetailInfo.LocSelection, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2410_003", this.pubParasInfo.UserLanguage, new List<string> { putawayRuleDetailInfo.LocSelection });
                }
                if (putawayRuleDetailInfo == null)
                {
                    return LineID;
                }
                putawayRuleDetailInfo.UpdatedBy = putawayRuleDetailInfo.CreatedBy;
                putawayRuleDetailInfo.CreatedDate = DateTime.Now;
                putawayRuleDetailInfo.UpdatedDate = DateTime.Now;
                if (putawayDetailRuleDA.InsertPutawayRuleDetail(obj as PutawayRuleDetailInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<PutawayRuleDetailInfo>(null, putawayRuleDetailInfo, AuditAction.Insert, this.pubParasInfo.UserID, "PUTAWAY_RULE_DETAIL", this.dataBaseID[0]);
                    LineID = putawayRuleDetailInfo.LineID.ToString();
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return LineID;
        }

        public string UpdateDetail(object obj)
        {
            string LineID = string.Empty;
            try
            {
                PutawayRuleDetailInfo putawayRuleDetailInfo = obj as PutawayRuleDetailInfo;
                if (!string.IsNullOrEmpty(putawayRuleDetailInfo.LocSelection) && !filterConfigDetail.CheckFilterConfigDetailIDUnique(putawayRuleDetailInfo.LocSelection, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2410_004", this.pubParasInfo.UserLanguage, new List<string> { putawayRuleDetailInfo.LocSelection });
                }
                PutawayRuleDetailInfo oldPutawayRuleDetailInfo = putawayDetailRuleDA.GetPutawayRuleDetailByID(putawayRuleDetailInfo.PutawayRuleID, putawayRuleDetailInfo.LineID.ToString(), this.dataBaseID[0], this.pubParasInfo);
                if (putawayDetailRuleDA.UpdatePutawayRuleDetail(putawayRuleDetailInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<PutawayRuleDetailInfo>(oldPutawayRuleDetailInfo, putawayRuleDetailInfo, AuditAction.Update, this.pubParasInfo.UserID, "PUTAWAY_RULE_DETAIL", this.dataBaseID[0]);
                    LineID = putawayRuleDetailInfo.PutawayRuleID + " , " + putawayRuleDetailInfo.LineID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return LineID;
        }

        public string UpdateIsActive(List<string> mainID, List<string> subId, string isActive)
        {
            throw new NotImplementedException();
        }
    }
}

